const mysql = require("mysql");
const connection = mysql.createConnection({
  host: "127.0.0.1",
  user: "root",
  password: "root",
  database: "albx33"
});
module.exports = {
  getAllCategory(callback) {
    let sql = `SELECT * FROM categories`;
    connection.query(sql, (err, result) => {
      callback(err, result);
    });
  },
  addNewCategory(data, callback) {
    let sql = `INSERT INTO categories SET \`name\`='${data.name}',slug='${
      data.slug
    }',classname='${data.classname}'`;
    connection.query(sql, (err, result) => {
      callback(err, result);
    });
  },
  getCategoryById(id, callback) {
    let sql = `select * from categories where id = '${id}'`;
    connection.query(sql, (err, result) => {
      callback(err, result[0]);
    });
  },
  deleteCategoryById(id, callback) {
    let sql = `delete from categories where id = ${id}`;
    console.log(sql);
    connection.query(sql, (err, result) => {
      callback(err, result);
    });
  },
  editCategoryById(id, data, callback) {
    let sql = `update categories set ? where id=${id} `;
    connection.query(sql, data, (err, result) => {
      callback(err, result);
    });
  },
  deleteMultiple(ids, callback) {
    // console.log(ids);
    let data = ids.join();
    sql = `DELETE FROM categories WHERE id in (${data})`;
    connection.query(sql, (err, result) => {
      callback(err, result);
    });
  }
};
